iT邦幫忙

第 11 屆 iThome 鐵人賽

DAY 20
0

➤下拉式選單

html: 使用<select> <option>...<option> </select>放入選項,並給予value,提交type為submit。

<label for="drink">choose drink:</label>
        <select name="drink" id="drink">
            <option value="1">tea</option>
            <option value="2">milk</option>
            <option value="3">coffee</option>
            <option value="4">sangria</option>
            <option value="5">beer</option>
        </select>
        <input type="submit" name="send" value="送出">

php取得表單資料: 給予每個選項value後,可以利用value來做switch-case來執行結果。

<?php
    $result="";
    if(isset($_POST["send"])){
    $result = $_POST["drink"];
    switch($result){
        case 1:
        $result = "tea";
        break;
        case 2:
        $result = "milk";
        break;
        case 3:
        $result = "coffee";
        break;
        case 4:
        $result = "sangria";
        break;
        case 5:
        $result = "beer";
        break;
    }
    echo "$result";
    }
    ?> 

輸出:
https://ithelp.ithome.com.tw/upload/images/20191006/20120962U8KkG3rsnl.png


上一篇
Day19-PHP表單(2)
下一篇
Day21-PHP表單(4)
系列文
PHP入門30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言